package org.example.netty.test.nio.netty.chat.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.example.netty.test.nio.netty.chat.bean.*;
import org.example.netty.test.nio.netty.chat.common.MessageType;

/**
 * @author zp
 * @version 1.0
 * @description: 转化Handler 把对应的Message 转化为具体类型 方便后续的操作
 * @date 2023/4/5 21:28
 */
@Slf4j
public class ChangeMessageHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("class "+msg.getClass());
        if (msg instanceof Message) {
            Message message = (Message) msg;
            System.out.println("class "+message.getClass());
            switch (message.getMessageType()) {
                case MessageType.MESSAGE_TYPE_LOGIN:
                    ctx.fireChannelRead((LoginMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_LOG_OUT:
                    ctx.fireChannelRead((LogoutMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_SYSTEM:
                    ctx.fireChannelRead((SystemMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_SEND_USER:
                    ctx.fireChannelRead((SendUserMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_SEND_GROUP:
                    ctx.fireChannelRead((SendGroupMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_OUT_GROUP:
                    ctx.fireChannelRead((OutGroupMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_OUT_USER:
                    ctx.fireChannelRead((OutUserMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_JOIN_USER:
                    ctx.fireChannelRead((JoinUserMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_JOIN_GROUP:
                    ctx.fireChannelRead((JoinGroupMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_REGISTER:
                    ctx.fireChannelRead((RegisterMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_CREATE_GROUP:
                    ctx.fireChannelRead((CreateGroupMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_ALL_USER_LIST:
                    ctx.fireChannelRead((GetALLUserMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_ALL_GROUP_LIST:
                    ctx.fireChannelRead((GetALLGroupNameMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_ADD_GROUP_LIST:
                    ctx.fireChannelRead((GetAddGroupNameMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_ADD_USER_LIST:
                    ctx.fireChannelRead((GetAddUserMessage) message);
                    break;
                case MessageType.MESSAGE_TYPE_PING:
                    ctx.fireChannelRead((PingMessage) message);
                    break;
                default:
                    throw new Exception("消息转化异常");
            }
        } else {
            throw new Exception("数据转化异常 不是Message类型");
        }

    }
}
